package com.cdqidi.core.weixinmanager.util;

import com.cdqidi.dto.ApiResultWrapper;
import com.jfinal.weixin.sdk.api.ApiResult;

import java.util.Map;
import java.util.Set;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjk
 * @create 2019-07-01 10:59
 * @since 1.0.0
 */
public class WxApiResult {
    private WxApiResult() {

    }

    public static ApiResultWrapper<Object> setResponse(ApiResult apiResult) {
        if (apiResult.isSucceed()) {
            Map<String, Object> attrs = apiResult.getAttrs();
            Set<Map.Entry<String, Object>> set = attrs.entrySet();
            set.removeIf(entry -> "errcode".equals(entry.getKey()) || "errmsg".equals(entry.getKey()));
            if (attrs.isEmpty()) {
                return com.cdqidi.dto.ApiResult.success();
            }
            return com.cdqidi.dto.ApiResult.success(attrs);
        }
        return com.cdqidi.dto.ApiResult.error(apiResult.getErrorCode(), apiResult.getErrorMsg());
    }
}